package com.example.smartcity.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Entity.SSP;
import com.example.smartcity.Entity.SSP2;
import com.example.smartcity.Entity.User;
import com.example.smartcity.Mappers.SuiShouPaiMapper;
import com.example.smartcity.Mappers.UserMapper;
import com.example.smartcity.Service.SSPService;
import com.example.smartcity.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SSPServiceImpl implements SSPService {
    public String findAll(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        SuiShouPaiMapper suiShouPaiMapper = sqlSession.getMapper(SuiShouPaiMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<SSP> sspList = suiShouPaiMapper.GetAllSSP();
        List<SSP2> ssp2List = new ArrayList<SSP2>();
        for (SSP ssp : sspList){
            User user = userMapper.getUserByUid(ssp.getAuthorid());
            SSP2 ssp2 = new SSP2();
            ssp2.setAuthorname(user.getNickname());
            ssp2.setImgnames(ssp.getImgnames());
            ssp2.setContent(ssp.getContent());
            ssp2.setTitle(ssp.getTitle());
            ssp2List.add(ssp2);
        }

        JSONObject map = new JSONObject();
        map.put("sspList",ssp2List);
        System.out.println(ssp2List);
        return map.toJSONString();
    }
}
